using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web.Core.Objects;
using Web.Core;

public partial class AdminCP_AdminControls_AddEditCategory : System.Web.UI.UserControl
{
    public int catID = 0;
    public string header = "";
    string cDo = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        cDo = Request.Params["do"];
        if (cDo != null)
        {
            InitControl();          
        }
    }
    void InitControl()
    {                
        if ((int)Session["Language"] == GlobalStorage.Language.Vietnamese)
            this.scriptEpoch.InnerHtml = "<script type=\"text/javascript\" src=\"../Js/epoch_classes_vi.js\"></script>";
        else if ((int)Session["Language"] == GlobalStorage.Language.English)
            this.scriptEpoch.InnerHtml = "<script type=\"text/javascript\" src=\"../Js/epoch_classes.js\"></script>";        
        if (Request.Params["cid"] != null)
        {            
            catID = int.Parse(Request.Params["cid"]);
        }

        this.drpCat.Items.Add("(" + 0 + ")  No parent" );
        foreach (Category cat in Categories.GetParentCategories((int)Session["Language"]))
            this.drpCat.Items.Add("(" + cat.Id + ")  " + cat.Name);

        foreach (Manufacturer man in Manufacturers.GetAllManufacturers())
            this.drpMan.Items.Add("(" + man.MID + ")  " + man.MName);
        this.drpTop.Items.Add(Resources.Lang.Yes);
        this.drpTop.Items.Add(Resources.Lang.No);

        switch (cDo)
        {
            case "edit":
                header = Resources.Lang.Edit;
                Category catEn = Categories.GetCategoryByID(catID,GlobalStorage.Language.English);
                Category catVi = Categories.GetCategoryByID(catID, GlobalStorage.Language.Vietnamese);                
                this.txtNameEn.Text = catEn.Name;
                this.txtNameVi.Text = catVi.Name;
                this.txaDesCatEn.Value = catEn.Description;
                this.txaDesCatVi.Value = catVi.Description;
                this.txtImage.Text = catEn.Image;
                this.txtSequence.Text = catEn.Sequence.ToString();
                this.drpMan.Text = "(" + catEn.Manufact + ")  " + Manufacturers.GetManufacturersByID(catEn.Manufact).MName;
                if (catEn.Parent !=0)
                    this.drpCat.Text = "(" + catEn.Parent + ")  " + Categories.GetCategoryByID(catEn.Parent, (int)Session["Language"]).Name;
                else
                    this.drpCat.Text = "(" + 0 + ")  No parent";
                if(catEn.Top)
                    this.drpTop.SelectedIndex = 0;
                else
                    this.drpTop.SelectedIndex = 1;
                break;
            case "add":
                header = Resources.Lang.Add;
                break;
            default:
                break;
        }
        
        // Validator        
        this.RequiredFieldValidatorNameEn.ErrorMessage = Resources.Lang.RequireNameEn + ". ";
        this.RequiredFieldValidatorNameVi.ErrorMessage = Resources.Lang.RequiteNameVi + ".";
        this.RequiredFieldValidatortxaDesCatEn.ErrorMessage = Resources.Lang.Require;
        this.RequiredFieldValidatortxaDesCatVi.ErrorMessage = Resources.Lang.Require;
    }   
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string img = "";int sequence = 0;
        try{
        img = this.txtImage.Text;
        }
        catch{}
        try{
        sequence = int.Parse(this.txtSequence.Text);
        }
        catch{}

        int cID = int.Parse(this.drpCat.Text.Substring(1, this.drpCat.Text.IndexOf(")") - 1));
        int mID = int.Parse(this.drpMan.Text.Substring(1, this.drpMan.Text.IndexOf(")") - 1));
        bool top;

        if (this.drpTop.Text == Resources.Lang.Yes)
            top = true;
        else
            top = false;

        if (cDo == "edit")
        {
            Category cat = new Category(catID, img, sequence,cID,mID);
            cat.Top = top;
            ShopContent content = new ShopContent(this.txtNameEn.Text, this.txtNameVi.Text, this.txaDesCatEn.Value, this.txaDesCatVi.Value);
            Categories.UpdateCategory(cat, content);
        }
        else if (cDo == "add")
        {
            Category cat = new Category(catID, img, sequence, cID, mID);
            cat.Top = top;
            ShopContent content = new ShopContent(this.txtNameEn.Text, this.txtNameVi.Text, this.txaDesCatEn.Value, this.txaDesCatVi.Value);
            Categories.InsertCategory(cat, content);
        }
        Response.Redirect("Categories.aspx");
    }
}
